# 导入模块
from pymongo import MongoClient

# 实例化client建立连接并指定集合 当前mongo演示为本地
client = MongoClient(host='127.0.0.1', port=27017)
collection = client['mongo_data_info']['test_python']

# 插入一条数据
res = collection.insert_one({"name": "xiaoming", "age": 10})
print(res)

# 插入多条数据
data_list = [{"name": "test{}".format(i)} for i in range(10)]
collection.insert_many(data_list)

# 查询一条数据
t_1 = collection.find_one({"name": "xiaowang"})
print(t_1)

# 查询所有记录
t_2 = collection.find({"name": "xiaowang"})
# 当前返回的是一个游标对象
print(t_2)

for i in t_2:
    print(i)

# 对游标对象进行列表类型转换
print(list(t_2))

# 更新一条数据
collection.update_one({"name": "test1"}, {"$set": {"name": "new_test1"}})
# 更新全部数据
collection.update_many({"name": "test1"}, {"$set": {"name": "new_test1"}})

# 删除一条数据
collection.delete_one({"name": "test10"})
# 删除全部数据
collection.delete_many({"name": "test9"})